package com.diko.ucenter.controller;


import com.diko.commonUtils.JwtUtils;
import com.diko.commonUtils.R;
import com.diko.config.exceptionhandler.myException;
import com.diko.ucenter.entity.Member2;
import com.diko.ucenter.entity.vo.LoginVo;
import com.diko.ucenter.entity.vo.RegisterVo;
import com.diko.ucenter.service.Member2Service;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Emma-Li
 * @since 2021-03-28
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/member2")
public class Member2Controller {
    @Autowired
    private Member2Service member2Service;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        String token=member2Service.login(loginVo);
        System.out.println(R.ok().data("token",token));
        return R.ok().data("token",token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        member2Service.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            Member2 member2 = member2Service.getLoginInfo(memberId);
            return R.ok().data("item", member2);
        }catch (Exception e){
            e.printStackTrace();
            throw new myException(20001,"token获取信息失败");
        }
    }
}

